-- LOCALS WEEEEE~~!
local g = getfenv(0)
local addon = DongleStub"Dongle-1.0":New"ItemBid"
local tostring = tostring
local function _tostring(v, ...) if select('#', ...) == 0 then return tostring(v) end return tostring(v).." ".._tostring(...) end
local print = function(...) ChatFrame1:AddMessage(_tostring(...)) end

function addon:Enable()
	self.db = addon:InitializeDB"ItemBidDB"
	self.profile = self.db.profile
	if not self.profile then self.profile = {} end
	if not self.profile.temp then self.profile.temp = {} end
	if not self.profile.auctions then self.profile.auctions = {} end
	local slash = addon:InitializeSlashCommand("Item Bid Start", "ITEMBID", "itembid", "bid")
	slash:RegisterSlashHandler("start <item> - Starts a auction.", "start (.+)", "StartBid")
	slash:RegisterSlashHandler("close - Closes a auction. Is triggered when saying \"sold\".", "close", "CloseBid")
	slash:RegisterSlashHandler("current - Shows the current highest bid.","current", "CurrentHighest")
	slash:RegisterSlashHandler("change <bidder> <bid> - Changes the current bidder/bid.", "change (.+) (.+)", "ChangeCurrentBid")
	slash:RegisterSlashHandler("saved <id> <bidder> <bid> - Changes a saved bid.", "saved (.+) (.+) (.+)", "ChangeSavedBid")
	slash:RegisterSlashHandler("list - Lists saved bids.", "list", "List")
	slash:RegisterSlashHandler("remove <id> - Removes a saved bid.", "remove (.+)", "RemoveBid")
	slash:RegisterSlashHandler("reset - Resets the entire database.", "reset", "Reset")
end

function addon:StartBid(i)
	self.active = 1
	self.item = i
	SendChatMessage(i.." BID!", "RAID")
	self:RegisterEvent("CHAT_MSG_RAID", "OnBid")
	self:RegisterEvent("CHAT_MSG_RAID_LEADER", "OnBid")
	self.profile.temp.highbider = nil
	self.profile.temp.highbid = 0
end

function addon:OnBid(d,m,a)
	local c = string.match(m, "BID!")
	if c then return end
	local s = string.match(m, "sold")
	if s then self:CloseBid() return end
	local m = tonumber(string.match(m, "(%d+)"))
	local hbid = tonumber(self.profile.temp.highbid)
	if m then
		if m <= hbid or m < 10 or m > 700 then return end
			if a == self.profile.temp.highbider then return end
			self.profile.temp.highbider = a
			self.profile.temp.highbid = m
	end
end

function addon:CloseBid()
	if not self.active then self:Print"No current auctions running." return end
	self:UnregisterEvent"CHAT_MSG_RAID"
	self:UnregisterEvent"CHAT_MSG_RAID_LEADER"
	local t = time()
	local bidder = self.profile.temp.highbider
	local bid = tonumber(self.profile.temp.highbid)
	print("Saved bid for "..self.item..". Winner: "..bidder.." - "..bid.." DKP")
	table.insert(self.profile.auctions, { item = self.item, time = t, bidder = bidder, bid = bid })
	self.profile.temp.highbider = nil
	self.profile.temp.highbid = 0
	self.active = nil
end

function addon:List()
	if not self.profile.auctions then return self:Print"You dont have any saved auctions." end
	self:Print("Auctions:")
		for n,v in pairs(self.profile.auctions) do
			self:Print("["..n.."]: "..date("%x", v["time"])..": "..v["item"].." - "..v["bidder"].." - "..v["bid"].." DKP")
	end
end

function addon:CurrentHighest()
	if not self.active then self:Print"No current auctions running." return end
	if not self.profile.temp.highbider then self:Print"No bids made." return end
	self:Print(self.item..": "..self.profile.temp.highbider.." - "..self.profile.temp.highbid)
end

function addon:ChangeCurrentBid(hb, b)
	if not self.active then self:Print"No current auctions running." return end
		self.profile.temp.highbider = hb
		self.profile.temp.highbid = b
		self:Print("Changed bid to: "..hb.." - "..b)
end

function addon:ChangeSavedBid(id, hb, b)
	local id = tonumber(id)
	if not self.profile.auctions[id] then self:Print"Saved bid not found." return end
	self.profile.auctions[id].bidder = hb
	self.profile.auctions[id].bid = b
	self:Print("Changed bid for "..self.profile.auctions[id].item.." to: "..hb.." - "..b)
end

function addon:RemoveBid(id)
	local id = tonumber(id)
	if not self.profile.auctions[id] then self:Print"Saved bid not found." return end
	self:Print("Removed bid for "..self.profile.auctions[id].item..": "..self.profile.auctions[id].bidder.." - "..self.profile.auctions[id].bid)
	self.profile.auctions[id] = nil
end

function addon:Reset()
	self.db:ResetProfile()
	self.profile = {}
	self.profile.temp = {}
	self.profile.auctions = {}
end

g["itembid"] = addon
